package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyDetailService;
import cn.wolfcode.trip.base.service.IDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/dailyDetail")
public class DailyDetailController {
    @Autowired
    private IDailyDetailService dailyDetailService;
    @Autowired
    private IDailyService dailyService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        qo.setOrderBy(" sd.releaseTime desc");
        model.addAttribute("pageInfo", dailyDetailService.query(qo));
        model.addAttribute("dailyIds", dailyService.listEnable());
        return "dailyDetail/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(DailyDetail entity, MultipartFile img) {
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = UploadUtil.uploadQiniu(img);
            entity.setCoverUrl(url);
        }
        dailyDetailService.saveOrUpdate(entity);
        return "redirect:/dailyDetail/list.do";
    }

    @RequestMapping("/getContent")
    @ResponseBody
    public Object getContent(Long id) {
        return dailyDetailService.getContent(id);
    }
}
